package backend

import (
	servModels "awesomeProject1/models/service"
	"awesomeProject1/pkg/e"
	"awesomeProject1/pkg/gintool"
	"github.com/simplechain-org/client/log"
	"strings"
)

func (b *backend) FindOrgInfo(subBankIDList []string) (orgMappingInfoList []*servModels.OrgMappingInfo) {
	orgMappingInfoList = make([]*servModels.OrgMappingInfo, len(subBankIDList))

	for i, subOrgName := range subBankIDList {
		index := -1
		bankName := ""
		orgMappingInfo := &servModels.OrgMappingInfo{
			SubOrgName: subOrgName,
		}
		orgMappingInfoList[i] = orgMappingInfo

		index = strings.Index(subOrgName, "银行")
		if index > 0 {
			bankName = subOrgName[:index] + "银行"
		} else {
			index = strings.Index(subOrgName, "社")
			if index > 0 {
				bankName = subOrgName[:index] + "社"
			} else {
				index = strings.Index(subOrgName, "会")
				if index > 0 {
					bankName = subOrgName[:index] + "会"
				} else {
					index = strings.Index(subOrgName, "监")
					if index > 0 {
						bankName = subOrgName[:index] + "监"
					}
				}
			}
		}

		//对于输入的银行特殊处理
		slsBanks := getSlsBanks()
		if _, ok := slsBanks[bankName]; ok {
			bankName = "浙江农村商业联合银行"
		}

		list, _, code := b.organizationService.GetOrganizationList(bankName, gintool.CreatePager(1, 10))
		if code != e.Ok {
			//gintool.ResultCode(ctx, code)
			log.Warn("通过关键字获取机构列表出错", "bankName", subOrgName, "err", e.GetMsg(code))
			continue
		}
		if len(list) < 1 {
			bankNameRune := []rune(bankName)
			bankNameRuneLen := len(bankNameRune)

			if bankNameRuneLen > 2 {
				shortBankName := string(bankNameRune[2:])
				list2, _, code := b.organizationService.GetOrganizationList(shortBankName, gintool.CreatePager(1, 10))
				if code != e.Ok {
					//gintool.ResultCode(ctx, code)
					log.Warn("通过关键字获取机构列表出错", "bankName", subOrgName, "err", e.GetMsg(code))
					continue
				}

				if len(list2) > 1 {
					for _, item := range list2 {
						if strings.Contains(item.OrgName, "作废") {
							//ignore
						} else if orgMappingInfo.OrgName != "" {
							orgMappingInfo.OrgName += ", " + item.OrgName
							orgMappingInfo.OrgID = "错误，匹配到重复的省级银行。"
						} else {
							orgMappingInfo.OrgName = item.OrgName
							orgMappingInfo.OrgID = item.Code
						}
					}
				}
				if len(list2) < 1 {
					if bankNameRuneLen > 4 {
						shortBankName := string(bankNameRune[4:])
						list2, _, code = b.organizationService.GetOrganizationList(shortBankName, gintool.CreatePager(1, 10))
						if code != e.Ok {
							//gintool.ResultCode(ctx, code)
							log.Warn("通过关键字获取机构列表出错", "bankName", subOrgName, "err", e.GetMsg(code))
							continue
						}

						if len(list2) > 1 {
							for _, item := range list2 {
								if strings.Contains(item.OrgName, "作废") {
									//ignore
								} else if orgMappingInfo.OrgName != "" {
									orgMappingInfo.OrgName += ", " + item.OrgName
									orgMappingInfo.OrgID = "错误，匹配到重复的省级银行。"
								} else {
									orgMappingInfo.OrgName = item.OrgName
									orgMappingInfo.OrgID = item.Code
								}
							}
						}
						if len(list2) < 1 {
							continue
						}
					}
				}
			}

		}
		if len(list) > 1 {
			for _, item := range list {
				if strings.Contains(item.OrgName, "作废") {
					//ignore
				} else if orgMappingInfo.OrgName != "" {
					orgMappingInfo.OrgName += ", " + item.OrgName
					orgMappingInfo.OrgID = "错误，匹配到重复的省级银行。"
				} else {
					orgMappingInfo.OrgName = item.OrgName
					orgMappingInfo.OrgID = item.Code
				}
			}
		}
		if len(list) == 1 {
			orgMappingInfo.OrgName = list[0].OrgName
			orgMappingInfo.OrgID = list[0].Code
		}
	}

	return
}
